<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="com.yaodian100.crm.workflow.entity.WorkFlowNode" %>
<%@taglib uri="/WEB-INF/tld/pageutil.tld" prefix="pu"%>
<%@taglib uri="/WEB-INF/tld/pageable.tld" prefix="pageable"%>
<%@taglib uri="http://java.sun.com/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jstl/core_rt" prefix="c-rt"%>
<%@taglib uri="http://java.sun.com/jstl/fmt_rt" prefix="fmt-rt"%>

<%!
void buildTree(Object[] objs, StringBuffer sb, int count, ServletContext application){
    count++;
    if(count>10)return;
    if(objs!=null && objs.length>0){
		sb.append("<ul>"); 
	    for(Object obj:objs){
			WorkFlowNode wfn=(WorkFlowNode)obj;
			System.out.println("wfn:"+wfn.getShortDesc());
			sb.append("<li id=\""+wfn.getId()+"\" rel=\"processNode\"><a href=\"#\" onclick=\"doAR('"+application.getContextPath()+"/workFlow/page.do?iamlazy=workFlowNodeDetail&id="+wfn.getId()+"', 'rightSide')\"><ins>&nbsp;</ins>"+wfn.getShortDesc()+"</a>"); 
			if(wfn.getChilds()!=null && wfn.getChilds().size()>0){
			    buildTree(wfn.getChilds().toArray(), sb, count, application);
			}
			sb.append("</li>");
	    }
	    sb.append("</ul>");
    }
}
%>
<c-rt:set var="workFlows" value="${pu:queryByHql('from WorkFlow order by name asc', null, null)}"/>

		
			<script type="text/javascript" class="source"> 
	$(function () { 
		
		$("#leftSide").tree(
			{
				rules : {
				// only nodes of type root can be top level nodes
				valid_children : [ "root" ]
				},
				types : {
				// all node types inherit the "default" node type
				"default" : {
					deletable : false,
					renameable : false,
					draggable: false
				},
				"root" : {
					draggable : false,
					valid_children : [ "process" ],
					icon : { 
						image : "<%=application.getContextPath() %>/images/FAMFAMFAM/application_form_add.png"
					}
				},
				"process" : {
					draggable : false,
					valid_children : [ "processNode" ],
					icon : { 
						image : "<%=application.getContextPath() %>/images/FAMFAMFAM/application_form.png"
					}
				},
				"processNode" : {
					valid_children : [ "processNode" ],
					icon : { 
						image : "<%=application.getContextPath() %>/images/FAMFAMFAM/group.png"
					}
				}
				}
			}
		);
		var focused='${param.focused}';
		if(focused!=''){
			$.tree.focused().open_branch("#theFlowTree");	
			$.tree.focused().open_all("#theFlowTree");
			$.tree.focused().open_branch("#"+focused);
			$.tree.focused().open_all("#"+focused);
		}
		$('#theTree').show();	
	});
	
	</script> 
	
		<ul id="theTree" style="display:none"> 
			<li id="theFlowTree" rel="root" class="open"><a href="#" onclick="doAR('<%=application.getContextPath() %>/workFlow/page.do?iamlazy=toUpdateFlow', 'rightSide')">工單流程</a>
			<c-rt:if test="${!empty workFlows}"> 
				<ul> 
					<c-rt:forEach items="${workFlows }" var="wo">
					<li id="${wo.name }" rel="process"><a href="#" onclick="doAR('<%=application.getContextPath() %>/workFlow/page.do?iamlazy=workFlowDetail&name=${wo.name}', 'rightSide')"><ins>&nbsp;</ins>${wo.description }</a>
					<c-rt:set var="params" value="${pu:addToList('paras', wo.name)}"/>
					<c-rt:set var="wons" value="${pu:queryByHql('from WorkFlowNode where workFlow.name=? and parent is null', params, null)}"/>
					<c-rt:if test="${!empty wons}">
						<%
						Object[] wons=(Object[])pageContext.getAttribute("wons");
						StringBuffer sb=new StringBuffer("");
						buildTree(wons, sb, 0, application);
						out.print(sb.toString());
						
						%>
					</c-rt:if>
					</li> 
					<%
					pageContext.setAttribute("params", null);
					request.setAttribute("paras", null);
					%>
					</c-rt:forEach>
				</ul>
				</c-rt:if> 
			</li> 
			
		</ul> 

	